package com.core.framework.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;


public class SpringContextUtils {
	protected static org.slf4j.Logger logger = org.slf4j.LoggerFactory
			.getLogger(SpringContextUtils.class);
	public static void main(String[] args)
	{
			
	}
	Integer loadCount;

	protected ConfigurableApplicationContext loadContext(Object key)
			throws Exception {
		return loadContextLocations((String[]) key);
	}

	protected ConfigurableApplicationContext loadContextLocations(
			String[] locations) throws Exception {
//		++this.loadCount;
//		if (this.logger.isInfoEnabled()) {
//			this.logger.info("Loading context for locations: "
//					+ StringUtils.arrayToCommaDelimitedString(locations));
//		}
		return createApplicationContext(locations);
	}

	protected ConfigurableApplicationContext createApplicationContext(
			final String[] locations) {
		GenericApplicationContext context = new GenericApplicationContext();
		// customizeBeanFactory(context.getDefaultListableBeanFactory());
		///context.loadBeanDefinitions(locations);
		//context.getDefaultListableBeanFactory().autowire(A1Test.class, 1, true);
		context.refresh();
		return context;
	}

	protected String[] getConfigLocations() {
		String[] paths = getConfigPaths();
		String[] locations = new String[paths.length];
		for (int i = 0; i < paths.length; i++) {
			String path = paths[i];
			if (path.startsWith("/")) {
				locations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
			} else {
				locations[i] = ResourceUtils.CLASSPATH_URL_PREFIX
						+ StringUtils.cleanPath(ClassUtils
								.classPackageAsResourcePath(getClass())
								+ "/" + path);
			}
		}
		return locations;
	}

	protected String[] getConfigPaths() {
		return new String[] { "classpath:applicationContext-resources.xml",
				"classpath:WEB-INF/applicationContext.xml",
				"classpath:applicationContext-dao.xml" };
	}
}
